#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void bfs(vector<vector<int>> adj, int start) {
    int m = adj.size();
    vector<bool> visited(m, false);
    queue<int> nodeQueue;
    visited[start] = true;
    nodeQueue.push(start);

    while (!nodeQueue.empty()) {
        int cur = nodeQueue.front();
        nodeQueue.pop();
        // visited[cur] = true;
        cout << cur << " ";

        for (int n : adj[cur]) {
            if (!visited[n]) {
                visited[n] = true;
                nodeQueue.push(n);
            }
        }
    }
}

int main() {
    vector<vector<int>> adj = {
        {1, 2, 3},
        {0, 2},
        {0, 1, 4},
        {0, 4},
        {2, 3}
    };
    bfs(adj, 0);
    return 0;
}